<?php

class TemplateAdmin
{
    private $view;
    private $data = array();
    private $viewFile;
    private $layoutFile;

    public function __construct($view, $data)
    {
        $this->view = $view;
        $this->data = $data;
        $this->viewFile = $_SERVER['DOCUMENT_ROOT'].'/admin/templates/'.$this->view.'.phtml';
        $this->layoutFile = $_SERVER['DOCUMENT_ROOT'].'/admin/templates/layout.phtml';
    }
    
    public function viewTemplate()
    {
        $str = $this->getString();
        echo $str;
    }
    
    public function onlyView()
    {
        $str = $this->getString(1);
        echo $str;
    }
    
    private function getString($only = null)
    {
        if (!is_readable($this->viewFile))
        {
            die('View not found');
        }
        
        $data = $this->data;
        $entity = new EntityAdmin();
        $data['entities'] = $entity->entitiesList();    
        ob_start();
        include ($this->viewFile);
        $content = ob_get_clean();
        
        if ($only)
        {
            return $content;
        }
        
        ob_start();
        include ($this->layoutFile);
        $content = ob_get_clean();
        
        return $content;
    }
}